//
//  Customer_UITabBar.m
//  Demo
//
//  Created by 王跃 on 2017/12/27.
//  Copyright © 2017年 WY. All rights reserved.
//

#import "Customer_UITabBar.h"
#import "Customer_TabBarButton.h"
#import "CustomerMiddleButton.h"

#define BUTTONINDEX 1000


@implementation Customer_UITabBar
{
     NSInteger      _selectedButtonIndex;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self){

        self.backgroundColor = [UIColor clearColor];
        _defaultSelected = 1;
        _selectedButtonIndex = 1;

    }
    return self;
}

- (UIImageView *)imageView
{
    CGFloat width = self.frame.size.width;
    UIImageView *imageView = [[UIImageView alloc]init];
     imageView.frame = CGRectMake(0,
                                  -15,
                                  width,
                                  64);

    imageView.image = [UIImage imageNamed:@"底部栏"];
    return imageView;
}

- (void)setTitleArray:(NSArray *)titleArray
{
    _titleArray = titleArray;
    [self setupUI];
}

- (void)setDefaultSelected:(NSInteger)defaultSelected
{
    _defaultSelected = defaultSelected;
    _selectedButtonIndex = defaultSelected;
     [self setupUI];
}

#pragma mark 设置UI

- (void)setupUI
{
    for (UIView *view in self.subviews){
        [view removeFromSuperview];
    }
    //图片最好是750 * 118   @2x的
    [self addSubview:self.imageView];
    for (int i = 0; i < self.titleArray.count; i++)
    {
        if (i != 2){

            Customer_TabBarButton *button = [Customer_TabBarButton buttonWithType:UIButtonTypeCustom];
            [self setButton:button andindex:i];

        }else{

            CustomerMiddleButton *button = [CustomerMiddleButton buttonWithType:UIButtonTypeCustom];

            [self setButton:button
                   andindex:i
             ];
        }
    }
}

#pragma mark 设置button

 - (void)setButton:(UIButton *)button andindex:(NSInteger)i
 {
    if (i == _defaultSelected){

        button.selected = YES;
    }
    else{

        button.selected = NO;
    }
    [button setTitle:self.titleArray[i] forState:UIControlStateNormal];
    button.titleLabel.textAlignment = NSTextAlignmentCenter;
    button.backgroundColor = [UIColor clearColor];
    //这里修改选中字体和图片
    [button setTitleColor:[UIColor redColor]
                 forState:UIControlStateSelected];

    [button setTitleColor:[UIColor grayColor]
                 forState:UIControlStateNormal];

    [button setImage:[UIImage imageNamed:self.titleArray[i]]
            forState:UIControlStateNormal];

    button.titleLabel.font = [UIFont systemFontOfSize:10.f];
     //设置标记
    button.tag = BUTTONINDEX + i;
    button.highlighted = NO;
    [button setImage:[UIImage imageNamed:[self.titleArray[i]
            stringByAppendingString:@"_highlight"]] forState:UIControlStateSelected];

    [button addTarget:self
               action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:button];
 }

#pragma mark 点击事件

 - (void)clickButton:(UIButton *)button
 {
     if (_selectedButtonIndex == button.tag){
         return;
     }

    for (int i = 0; i < self.titleArray.count; i++){

        UIButton *buttons = [self viewWithTag:BUTTONINDEX + i];
        buttons.selected = NO;

    }

    button.selected = YES;
    _selectedButtonIndex = button.tag;

    if (self.delegate && [self.delegate respondsToSelector:@selector(tabbar:clickButton:)]){

        [self.delegate tabbar:self clickButton:button.tag - BUTTONINDEX];

    }
 }

#pragma mark 初始化布局

 - (void)layoutSubviews
 {
    if (self.titleArray.count == 0 || self.titleArray.count > 5)
    {
        return;
    }

    CGFloat width = self.frame.size.width/self.titleArray.count;

    for (int i = 0; i < self.titleArray.count; i++)
    {
        UIButton *button = [self viewWithTag:BUTTONINDEX + i];
        button.frame = CGRectMake(width * i,
                                  5,
                                  width,
                                  self.frame.size.height);

        if (2 == i){
            button.frame = CGRectMake(width * i,
                                      -5,
                                      width,
                                      self.frame.size.height);
        }
    }
 }
@end
